<?php

namespace App\Http\Middleware;

use Closure;
use Cache;

class VaildMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $askCode = $request->input('askCode');
        $stuname = $request->input('stuname');
        $stunum = $request->input('stunum');
        $index = 1;
        $stuLogging = array();
        if(Cache::has($askCode.'stuLogged'))
        {
            $stuLogging = Cache::pull($askCode.'stuLogged');
            if(in_array($stuname,$stuLogging))
            {
                if(Cache::has($askCode.'nowIndex'))
                {
                    $index = Cache::get($askCode.'nowIndex');
                }else{
                    $index = 1;
                }
                Cache::add($askCode.'stuLogged',$stuLogging,100);
                return response()->json(["state" => '4', "index"=>$index,"error"=>"你已重复登录成功"]);
            }else{
                return $next($request);
            }
        }else{
            return $next($request);
        }
    }
}
